########################################################
### Varieties of Economic Vulnerability  				 			 
### Melina Altamirano, Sarah Berens & Franziska Deeg 	 			 
### March 2021         											 
### Conjoint Analysis
########################################################

library(dotwhisker)
library(magrittr)

####General Results: Figure B Appendix: Conjoint: Results for Average Respondent
coef.general <- c(0, 1.6401e-03, 1.3663e-02, 1.8668e-02, 1.3301e-02,
                  0, -1.9657e-02, 5.6546e-03, 
                  0, -1.3006e-02, 1.7481e-02, 
                  0, 7.0676e-05, 8.2656e-03) 

se.general <- c(0, 0.019584, 0.019533, 0.019280, 0.019779,
                0, 0.015130, 0.014605, 
                0, 0.015109, 0.014702, 
                0, 0.015092, 0.014808) 

var.names <- c("Baseline: Health Care", "Pensions", "Housing Credit", "Targeted poor households", "Child Care",
               "Baseline: Contribution based", "Universal", "Targeted", 
               "Baseline: Keep benefit same", "Increase benefit", "Reduce benefit", 
               "Baseline: Contributions", "Increase tax", "Increase in rich taxing") 

results_general <- data.frame(term = rep(var.names, times = 1),
                                     estimate = c(coef.general),
                                     std.error = c(se.general),
                                     stringsAsFactors = FALSE)

four_brackets<-list(c("Policy", "Baseline: Health Care", "Child Care"),
                    c("Access", "Baseline: Contribution based", "Targeted"), 
                    c("Benefits", "Baseline: Keep benefit same", "Reduce benefit"),
                    c("Finance", "Baseline: Contributions", "Increase in rich taxing"))


general <- dwplot(results_general, dot_args = list(aes(shape = model)), whisker_args = list(aes(linetype = model))) +
  theme_bw() +
  theme(plot.margin = margin(2, 2, .1, 1, "cm"), legend.position = "none") + #, legend.position="bottom",
        #legend.title = element_blank(), legend.background =
          #element_rect(color="grey60")) + 
  xlab("Change in E[Y]") +
  scale_colour_grey()+
  geom_vline(xintercept = 0, colour = "grey60", linetype = 2) 

general

general %>% add_brackets(list(c("Policy", "Pensions", "Child Care"),
                             c("Access", "Universal", "Targeted"), 
                             c("Benefits", "Increase benefit", "Reduce benefit"),
                             c("Finance", "Increase tax", "Increase in rich taxing")))


###Figure 4: Conjoint: Formal and Informal Sector Workers
coef.formal <- c(0, -0.0310244, 0.0093147, -0.0048189, 0.0139267,
                 0, -0.0076955, 0.0470764, 
                 0, -0.0400336, 0.0021398, 
                 0, -0.0404157, -0.0191645) 
                 
se.formal <- c(0, 0.042869, 0.043078, 0.041884, 0.042776,
               0, 0.031626, 0.031295, 
               0, 0.032442, 0.029957, 
               0, 0.032407, 0.032503) 
               
coef.informal <- c(0, -0.0273120, -0.0072836, -0.0139832, -0.0263648,
                    0, -0.0210050, -0.0059960, 
                   0, 0.0015891, 0.0250468, 
                   0, 0.0176300, 0.0294632) 
                   
se.informal <- c(0, 0.032007, 0.033184, 0.031630, 0.032861,
                 0, 0.024478, 0.024650, 
                 0, 0.025145, 0.024807, 
                 0, 0.025451, 0.024601) 
                 
var.names <- c("Baseline: Health Care", "Pensions", "Housing Credit", "Targeted poor households", "Child Care",
               "Baseline: Contribution based", "Universal", "Targeted", 
               "Baseline: Keep benefit same", "Increase benefit", "Reduce benefit", 
               "Baseline: Contributions", "Increase tax", "Increase in rich taxing") 


results_formalinformal <- data.frame(term = rep(var.names, times = 2),
                         estimate = c(coef.formal, coef.informal),
                         std.error = c(se.formal, se.informal),
                         model = c(rep("Formal", 14), rep("Informal", 14)),
                         stringsAsFactors = FALSE)

four_brackets<-list(c("Policy", "Baseline: Health Care", "Child Care"),
                    c("Access", "Baseline: Contribution based", "Targeted"), 
                    c("Benefits", "Baseline: Keep benefit same", "Reduce benefit"),
                    c("Finance", "Baseline: Contributions", "Increase in rich taxing"))


forinf <- dwplot(results_formalinformal, dot_args = list(aes(shape = model)), whisker_args = list(aes(linetype = model))) +
  theme_bw() +
  theme(plot.margin = margin(2, 2, .1, 1, "cm"), legend.position="bottom",
        legend.title = element_blank(), legend.background =
          element_rect(color="grey60")) + 
  xlab("Change in E[Y]") +
  scale_colour_grey(start = 0.2,
                    end = 0.6,)+
  geom_vline(xintercept = 0, colour = "grey60", linetype = 2) 

forinf


forinf %>% add_brackets(list(c("Policy", "Pensions", "Child Care"),
                             c("Access", "Universal", "Targeted"), 
                            c("Benefits", "Increase benefit", "Reduce benefit"),
                            c("Finance", "Increase tax", "Increase in rich taxing")))
                        

###Figure 5: Conjoint: Future Employment Expectations
coef.fortoinf <- c(0, -0.0407582, -0.0333424, -0.0235348, -0.0370594,
                   0, -0.0674496, -0.0025084, 
                   0, -0.1102599, -0.0671590, 
                   0, -0.0022582, 0.0148903) 
                   
se.fortoinf <- c(0, 0.069348, 0.067707, 0.064896, 0.066694,
                 0, 0.045143, 0.046621, 
                 0, 0.048733, 0.045625, 
                 0, 0.051534, 0.056801) 
                 
coef.inftofor <- c(0, -0.07286442, -0.10564417, 0.02332409, -0.00063419,
                   0, -0.05197975, -0.06030290, 
                   0, -0.06419250, 0.01606711, 
                   0, 0.11683638, 0.11803619) 
                   
se.inftofor <- c(0, 0.058122, 0.057812, 0.056227, 0.060679,
                 0, 0.042333, 0.043145, 
                 0, 0.043503, 0.036950, 
                 0, 0.038308, 0.040688) 
                 
var.names <- c("Baseline: Health Care", "Pensions", "Housing Credit", "Targeted poor households", "Child Care",
               "Baseline: Contribution based", "Universal", "Targeted", 
               "Baseline: Keep benefit same", "Increase benefit", "Reduce benefit", 
               "Baseline: Contributions", "Increase tax", "Increase in rich taxing") 


results_labor_persp <- data.frame(term = rep(var.names, times = 2),
                                     estimate = c(coef.fortoinf, coef.inftofor),
                                     std.error = c(se.fortoinf, se.inftofor),
                                     model = c(rep("Likely Formal to Informal", 14), rep("Likely Informal to Formal", 14)),
                                     stringsAsFactors = FALSE)

four_brackets<-list(c("Policy", "Baseline: Health Care", "Child Care"),
                    c("Access", "Baseline: Contribution based", "Targeted"), 
                    c("Benefits", "Baseline: Keep benefit same", "Reduce benefit"),
                    c("Finance", "Baseline: Contributions", "Increase in rich taxing"))


labor_persp <- dwplot(results_labor_persp, dot_args = list(aes(shape = model)), whisker_args = list(aes(linetype = model))) +
  theme_bw() +
  theme(plot.margin = margin(2, 2, .1, 1, "cm"), legend.position="bottom",
        legend.title = element_blank(), legend.background =
          element_rect(color="grey60")) + 
  xlab("Change in E[Y]") +
  scale_colour_grey(start = 0.2,
                    end = 0.6,)+
  geom_vline(xintercept = 0, colour = "grey60", linetype = 2) 

labor_persp

labor_persp %>% add_brackets(list(c("Policy", "Pensions", "Child Care"),
                                  c("Access", "Universal", "Targeted"), 
                                c("Benefits", "Increase benefit", "Reduce benefit"),
                                c("Finance", "Increase tax", "Increase in rich taxing")))
                             

###Figure E Appendix: Conjoint: Household Constellations: Purely Formal HH and Informal HH
coef.purinf <- c(0, 0.0005396, -0.0111103, 0.0130772, -0.0216175,
                 0, -0.0306863, -0.0098264, 
                 0, 0.0041498, 0.0116971, 
                 0, 0.0342927, 0.0260388) 

se.purinf <- c(0, 0.036572, 0.038258, 0.036386, 0.038733,
               0, 0.027954, 0.028843, 
               0, 0.028557, 0.028606, 
               0, 0.028713, 0.027968) 

coef.purfor <- c(0, 0.0294746, 0.0525027, -0.0072549, 0.0808358,
                 0, -0.0208725, 0.0357087, 
                 0, -0.0294155, 0.0128177,
                 0, -0.0094307, 0.0018737)

se.purfor <- c(0, 0.058786, 0.059095, 0.057591, 0.055350,
               0, 0.045116, 0.043572, 
               0, 0.047680, 0.040822,
               0, 0.042324, 0.044773) 

var.names <- c("Baseline: Health Care", "Pensions", "Housing Credit", "Targeted poor households", "Child Care",
               "Baseline: Contribution based", "Universal", "Targeted", 
               "Baseline: Keep benefit same", "Increase benefit", "Reduce benefit", 
               "Baseline: Contributions", "Increase tax", "Increase in rich taxing") 


results_pur <- data.frame(term = rep(var.names, times = 2),
                                  estimate = c(coef.purinf, coef.purfor),
                                  std.error = c(se.purinf, se.purfor),
                                  model = c(rep("Purely Informal", 14), rep("Purely Formal", 14)),
                                  stringsAsFactors = FALSE)

four_brackets<-list(c("Policy", "Baseline: Health Care", "Child Care"),
                    c("Access", "Baseline: Contribution based", "Targeted"), 
                    c("Benefits", "Baseline: Keep benefit same", "Reduce benefit"),
                    c("Finance", "Baseline: Contributions", "Increase in rich taxing"))


pur <- dwplot(results_pur, dot_args = list(aes(shape = model)), whisker_args = list(aes(linetype = model))) +
  theme_bw() +
  theme(plot.margin = margin(2, 2, .1, 1, "cm"), legend.position="bottom",
        legend.title = element_blank(), legend.background =
          element_rect(color="grey60")) + 
  xlab("Change in E[Y]") +
  scale_colour_grey(start = 0.2,
                    end = 0.6,)+
  geom_vline(xintercept = 0, colour = "grey60", linetype = 2) 

pur

pur %>% add_brackets(list(c("Policy", "Pensions", "Child Care"),
                                  c("Access", "Universal", "Targeted"), 
                                  c("Benefits", "Increase benefit", "Reduce benefit"),
                                  c("Finance", "Increase tax", "Increase in rich taxing")))


###Figure F Appendix: Conjoint: Household Constellations: Mixed Households
coef.mixformal <- c(0, 0.0743762, -0.0312115, 0.0731454, -0.0078849,
                    0, -0.0715208, 0.0158084,
                    0, -0.0872481, 0.1386384,
                    0, 0.0403334, 0.0699851)

se.mixformal <- c(0, 0.104239, 0.099182, 0.118163, 0.126450,
                  0, 0.065824, 0.068973, 
                  0, 0.069525, 0.072058,
                  0, 0.084904, 0.078091) 

coef.mixinformal <- c(0, -0.1215763, 0.0025978, -0.1248797, -0.0069300,
                      0, -0.0380704, -0.0360200, 
                      0, -0.0381587, 0.1107576,
                      0, -0.0520379, 0.0351728)

se.mixinformal <- c(0, 0.088236, 0.096947, 0.093576, 0.085063,
                    0, 0.076049, 0.064867,
                    0, 0.066609, 0.062754,
                    0, 0.079318, 0.075396) 

var.names <- c("Baseline: Health Care", "Pensions", "Housing Credit", "Targeted poor households", "Child Care",
               "Baseline: Contribution based", "Universal", "Targeted", 
               "Baseline: Keep benefit same", "Increase benefit", "Reduce benefit", 
               "Baseline: Contributions", "Increase tax", "Increase in rich taxing") 


results_mixed <- data.frame(term = rep(var.names, times = 2),
                                  estimate = c(coef.mixformal, coef.mixinformal),
                                  std.error = c(se.mixformal, se.mixinformal),
                                  model = c(rep("Mixed, Formal Resp", 14), rep("Mixed, Informal Resp", 14)),
                                  stringsAsFactors = FALSE)

four_brackets<-list(c("Policy", "Baseline: Health Care", "Child Care"),
                    c("Access", "Baseline: Contribution based", "Targeted"), 
                    c("Benefits", "Baseline: Keep benefit same", "Reduce benefit"),
                    c("Finance", "Baseline: Contributions", "Increase in rich taxing"))


mixed <- dwplot(results_mixed, dot_args = list(aes(shape = model)), whisker_args = list(aes(linetype = model))) +
  theme_bw() +
  theme(plot.margin = margin(2, 2, .1, 1, "cm"), legend.position="bottom",
        legend.title = element_blank(), legend.background =
          element_rect(color="grey60")) + 
  xlab("Change in E[Y]") +
  scale_colour_grey(start = 0.2,
                    end = 0.6,)+
  geom_vline(xintercept = 0, colour = "grey60", linetype = 2) 

mixed

mixed %>% add_brackets(list(c("Policy", "Pensions", "Child Care"),
                                  c("Access", "Universal", "Targeted"), 
                                  c("Benefits", "Increase benefit", "Reduce benefit"),
                                  c("Finance", "Increase tax", "Increase in rich taxing")))


###Figure G Appendix: Conjoint: Vote Choice 
coef.PAN <- c(0, 0.0435139, 0.0849059, 0.0500625, 0.0717652,
              0, -0.0413642, -0.0124680,
              0, 0.0539760, 0.0228841,
              0, -0.0083100, 0.0058868)

se.PAN <- c(0, 0.045965, 0.044557, 0.044987, 0.044042,
            0, 0.036187, 0.035195, 
            0, 0.034280, 0.031066,
            0, 0.031858, 0.032861) 

coef.PRI <- c(0, 0.1646153, 0.0182737, 0.0810933, 0.0829337,
              0, -0.0621721, 0.0165327, 
              0, 0.0158283, 0.0670373,
              0, 0.0043800, -0.0053871)

se.PRI <- c(0, 0.081256, 0.071934, 0.079038, 0.089458,
            0, 0.061512, 0.049179,
            0, 0.062549, 0.058564,
            0, 0.058274, 0.058205) 

coef.Morena <- c(0, 0.0217277, -0.0182743, 0.0042686, 0.0044966,
                 0, -0.0084071, 0.0404052, 
                 0, -0.0522132, -0.0026171,
                 0, -0.0227846, -0.0166181)

se.Morena <- c(0, 0.033329, 0.034442, 0.033884, 0.034031,
               0, 0.026754, 0.026853,
               0, 0.028053, 0.027296,
               0, 0.028390, 0.025999)

var.names <- c("Baseline: Health Care", "Pensions", "Housing Credit", "Targeted poor households", "Child Care",
               "Baseline: Contribution based", "Universal", "Targeted", 
               "Baseline: Keep benefit same", "Increase benefit", "Reduce benefit", 
               "Baseline: Contributions", "Increase tax", "Increase in rich taxing") 


results_vote <- data.frame(term = rep(var.names, times = 3),
                            estimate = c(coef.PAN, coef.PRI, coef.Morena),
                            std.error = c(se.PAN, se.PRI, se.Morena),
                            model = c(rep("Vote Choice:PAN", 14), rep("Vote Choice:PRI", 14), rep("Vote Choice:Morena", 14)),
                            stringsAsFactors = FALSE)

four_brackets<-list(c("Policy", "Baseline: Health Care", "Child Care"),
                    c("Access", "Baseline: Contribution based", "Targeted"), 
                    c("Benefits", "Baseline: Keep benefit same", "Reduce benefit"),
                    c("Finance", "Baseline: Contributions", "Increase in rich taxing"))


vote <- dwplot(results_vote, dot_args = list(aes(shape = model)), whisker_args = list(aes(linetype = model))) +
  theme_bw() +
  theme(plot.margin = margin(2, 2, .1, 1, "cm"), legend.position="bottom",
        legend.title = element_blank(), legend.background =
          element_rect(color="grey60")) + 
  xlab("Change in E[Y]") +
  scale_colour_grey(start = 0.2,
                    end = 0.6,)+
  geom_vline(xintercept = 0, colour = "grey60", linetype = 3) 

vote

vote %>% add_brackets(list(c("Policy", "Pensions", "Child Care"),
                            c("Access", "Universal", "Targeted"), 
                            c("Benefits", "Increase benefit", "Reduce benefit"),
                            c("Finance", "Increase tax", "Increase in rich taxing")))



###Figure C Appendix: Conjoint: Interaction of Access and Financing 
coef.formalinter <- c(0, -0.0310244, 0.0093147, -0.0048189, 0.0139267,
                 0, -0.0076955, 0.0470764, 
                 0, -0.0400336, 0.0021398, 
                 0, -0.0404157, -0.0191645,
                 0, -0.105569, -0.155615, -0.031211, -0.162264) 

se.formalinter <- c(0, 0.042869, 0.043078, 0.041884, 0.042776,
               0, 0.031626, 0.031295, 
               0, 0.032442, 0.029957, 
               0, 0.032407, 0.032503,
               0, 0.079201, 0.072188, 0.072538, 0.072418) 

coef.informalinter <- c(0, -0.0273120, -0.0072836, -0.0139832, -0.0263648,
                   0, -0.0210050, -0.0059960, 
                   0, 0.0015891, 0.0250468, 
                   0, 0.0176300, 0.0294632,
                   0, 0.150157, 0.047406, 0.043367, 0.088121) 

se.informalinter <- c(0, 0.032007, 0.033184, 0.031630, 0.032861,
                 0, 0.024478, 0.024650, 
                 0, 0.025145, 0.024807, 
                 0, 0.025451, 0.024601,
                 0, 0.057352, 0.061173, 0.058673, 0.059080)  

var.names <- c("Baseline: Health Care", "Pensions", "Housing Credit", "Targeted poor households", "Child Care",
               "Baseline: Contribution based", "Universal", "Targeted", 
               "Baseline: Keep benefit same", "Increase benefit", "Reduce benefit", 
               "Baseline: Contributions", "Increase tax", "Increase in rich taxing",
               "Baseline: Contri. based:Contri.", "Universal:Increase tax", "Targeted:Increase tax", "Universal:Increase in rich taxing", "Targeted:Increase in rich taxing") 


results_inter_accessfin <- data.frame(term = rep(var.names, times = 2),
                            estimate = c(coef.formalinter, coef.informalinter),
                            std.error = c(se.formalinter, se.informalinter),
                            model = c(rep("Access:Finance, Formal", 19), rep("Access:Finance, Informal", 19)),
                            stringsAsFactors = FALSE)

four_brackets<-list(c("Policy", "Pensions", "Child Care"),
                    c("Access", "Universal", "Targeted"), 
                    c("Benefits", "Increase benefit", "Reduce benefit"),
                    c("Finance", "Increase tax", "Increase in rich taxing"),
                    c("Access:Finance", "Universal:Increase tax", "Targeted:Increase in rich taxing"))


inter_accessfin <- dwplot(results_inter_accessfin, dot_args = list(aes(shape = model)), whisker_args = list(aes(linetype = model))) +
  theme_bw() +
  theme(plot.margin = margin(2, 2, .1, 1, "cm"), legend.position="bottom",
        legend.title = element_blank(), legend.background =
          element_rect(color="grey60")) + 
  xlab("Change in E[Y]") +
  scale_colour_grey(start = 0.2,
                    end = 0.6,)+
  geom_vline(xintercept = 0, colour = "grey60", linetype = 2) 

inter_accessfin

inter_accessfin %>% add_brackets(list(c("Policy", "Pensions", "Child Care"),
                            c("Access", "Universal", "Targeted"), 
                            c("Benefits", "Increase benefit", "Reduce benefit"),
                            c("Finance", "Increase tax", "Increase in rich taxing"),
                            c("Access:Finance", "Universal:Increase tax", "Targeted:Increase in rich taxing")))

###Figure D Appendix: Conjoint: Interaction of Policy and Expansion vs Retrenchment 
coef.formalinter <- c(0, -0.0310244, 0.0093147, -0.0048189, 0.0139267,
                      0, -0.0076955, 0.0470764, 
                      0, -0.0400336, 0.0021398, 
                      0, -0.0404157, -0.0191645,
                      0, -0.0608264, 0.0539887, -0.0033308, -0.0366230, -0.2197514, -0.1362999, -0.0605227, 0.0371537) 

se.formalinter <- c(0, 0.042869, 0.043078, 0.041884, 0.042776,
                    0, 0.031626, 0.031295, 
                    0, 0.032442, 0.029957, 
                    0, 0.032407, 0.032503,
                    0, 0.096606, 0.098797, 0.104556, 0.099760, 0.098214, 0.097365, 0.100089, 0.106628) 

coef.informalinter <- c(0, -0.0273120, -0.0072836, -0.0139832, -0.0263648,
                        0, -0.0210050, -0.0059960, 
                        0, 0.0015891, 0.0250468, 
                        0, 0.0176300, 0.0294632,
                        0, -0.127294, -0.067697, -0.054954, -0.106333, -0.146182, -0.151636, -0.073717, -0.046658) 

se.informalinter <- c(0, 0.032007, 0.033184, 0.031630, 0.032861,
                      0, 0.024478, 0.024650, 
                      0, 0.025145, 0.024807, 
                      0, 0.025451, 0.024601,
                      0, 0.074365, 0.071230, 0.075615, 0.074914, 0.075656, 0.074790, 0.075052, 0.073365)  

var.names <- c("Baseline: Health Care", "Pensions", "Housing Credit", "Targeted poor households", "Child Care",
               "Baseline: Contribution based", "Universal", "Targeted", 
               "Baseline: Keep benefit same", "Increase benefit", "Reduce benefit", 
               "Baseline: Contributions", "Increase tax", "Increase in rich taxing",
               "Baseline: Keep same:Health Care", "Increase benefit:Pensions", "Reduce benefit:Pensions", "Increase benefit:Housing Credit", "Reduce benefit:Housing Credit", "Increase benefit:Targeted poor households", "Reduce benefit:Targeted poor households", "Increase benefit:Child Care", "Reduce benefit:Child Care") 

results_inter_policybene <- data.frame(term = rep(var.names, times = 2),
                                      estimate = c(coef.formalinter, coef.informalinter),
                                      std.error = c(se.formalinter, se.informalinter),
                                      model = c(rep("Policy:Benefits, Formal", 23), rep("Policy:Benefits, Informal", 23)),
                                      stringsAsFactors = FALSE)

four_brackets<-list(c("Policy", "Pensions", "Child Care"),
                    c("Access", "Universal", "Targeted"), 
                    c("Benefits", "Increase benefit", "Reduce benefit"),
                    c("Finance", "Increase tax", "Increase in rich taxing"),
                    c("Policy:Benefits", "Increase benefit:Pensions", "Reduce benefit:Child Care"))


inter_policybene <- dwplot(results_inter_policybene, dot_args = list(aes(shape = model)), whisker_args = list(aes(linetype = model))) +
  theme_bw() +
  theme(plot.margin = margin(2, 2, .1, 1, "cm"), legend.position="bottom",
        legend.title = element_blank(), legend.background =
          element_rect(color="grey60")) + 
  xlab("Change in E[Y]") +
  scale_colour_grey(start = 0.2,
                    end = 0.6,)+
  geom_vline(xintercept = 0, colour = "grey60", linetype = 2) 

inter_policybene

inter_policybene %>% add_brackets(list(c("Policy", "Pensions", "Child Care"),
                                      c("Access", "Universal", "Targeted"), 
                                      c("Benefits", "Increase benefit", "Reduce benefit"),
                                      c("Finance", "Increase tax", "Increase in rich taxing"),
                                      c("Policy:Benefits", "Increase benefit:Pensions", "Reduce benefit:Child Care")))

